<template>
  <div class="group-chat-container">
    <!-- 顶部导航栏 -->
    <div class="chat-header">
      <el-button icon="el-icon-arrow-left" @click="goBack" circle></el-button>
      <div class="group-info">
        <div class="group-name">{{ currentGroup.name }} ({{ currentGroup.memberCount }}/100)</div>
        <div class="group-members" @click="showGroupMembers">
          <i class="el-icon-user"></i>
          群成员({{ currentGroup.memberCount }})
        </div>
      </div>
      <div class="chat-actions">
        <el-button icon="el-icon-setting" circle @click="groupSettings"></el-button>
      </div>
    </div>
    
    <!-- 群公告 -->
    <div class="group-announcement">
      <div class="announcement-title">群公告</div>
      <div class="announcement-content">{{ currentGroup.announcement }}</div>
    </div>
    
    <!-- 消息显示区域 -->
    <div class="chat-messages" ref="messagesContainer">
      <div class="message-date">2024年1月15日</div>
      
      <!-- 群公告消息 -->
      <div class="message-item system">
        <div class="message-content">
          <div class="message-text">
            <i class="el-icon-bell"></i>
            欢迎新成员加入！请遵守群规，文明交流。
          </div>
        </div>
      </div>
      
      <!-- 群消息 -->
      <div class="message-item received">
        <img :src="groupMembers[1].avatar" :alt="groupMembers[1].name" class="message-avatar" />
        <div class="message-content">
          <div class="message-sender">{{ groupMembers[1].name }} (管理员)</div>
          <div class="message-text">@全体成员 明天下午2点开会</div>
          <div class="message-time">14:30</div>
        </div>
      </div>
      
      <!-- 自己的消息 -->
      <div class="message-item sent">
        <div class="message-content">
          <div class="message-text">通知收到，我会准时参加</div>
          <div class="message-time">14:32</div>
        </div>
      </div>
    </div>
    
    <!-- 底部输入区域 -->
    <div class="chat-input">
      <div class="input-actions">
        <el-button icon="el-icon-picture" circle @click="selectImage"></el-button>
        <el-button icon="el-icon-folder" circle @click="selectFile"></el-button>
        <el-button icon="el-icon-plus" circle @click="showExtraActions"></el-button>
      </div>
      <div class="input-container">
        <el-input
          type="textarea"
          v-model="messageContent"
          placeholder="请输入消息..."
          :rows="3"
          @keydown.ctrl.enter="addNewLine"
          @keydown.enter.exact.prevent="sendMessage"
        ></el-input>
      </div>
      <div class="send-actions">
        <el-dropdown @command="handleExtraAction">
          <el-button icon="el-icon-at" circle></el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item 
                v-for="member in groupMembers" 
                :key="member.id" 
                :command="member.id"
              >
                {{ member.name }}
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
        <el-button type="primary" @click="sendMessage">发送</el-button>
      </div>
    </div>
    
    <!-- 群成员列表对话框 -->
    <el-dialog title="群成员" v-model="membersDialogVisible" width="400px">
      <div class="members-list">
        <div class="member-item" v-for="member in groupMembers" :key="member.id">
          <img :src="member.avatar" :alt="member.name" class="member-avatar" />
          <div class="member-info">
            <div class="member-name">{{ member.name }}</div>
            <div class="member-role">{{ member.role }}</div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "GroupChat",
  data() {
    return {
      currentGroup: {
        id: 1,
        name: "技术交流群",
        memberCount: 15,
        announcement: "欢迎新成员加入！请遵守群规，文明交流。"
      },
      groupMembers: [
        { id: 1, name: "我", role: "群主", avatar: require('@/assets/default-avatar.png') },
        { id: 2, name: "李四", role: "管理员", avatar: require('@/assets/default-avatar.png') },
        { id: 3, name: "王五", role: "成员", avatar: require('@/assets/default-avatar.png') },
        { id: 4, name: "赵六", role: "成员", avatar: require('@/assets/default-avatar.png') },
        { id: 5, name: "钱七", role: "成员", avatar: require('@/assets/default-avatar.png') }
      ],
      messageContent: "",
      membersDialogVisible: false
    };
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    
    showGroupMembers() {
      this.membersDialogVisible = true;
    },
    
    groupSettings() {
      this.$message.info('群设置功能待实现');
    },
    
    selectImage() {
      this.$message.info('选择图片功能待实现');
    },
    
    selectFile() {
      this.$message.info('选择文件功能待实现');
    },
    
    showExtraActions() {
      this.$message.info('更多功能待实现');
    },
    
    handleExtraAction(command) {
      const member = this.groupMembers.find(m => m.id === command);
      if (member) {
        this.messageContent += `@${member.name} `;
      }
    },
    
    addNewLine() {
      this.messageContent += '\n';
    },
    
    sendMessage() {
      if (!this.messageContent.trim()) {
        this.$message.warning('请输入消息内容');
        return;
      }
      
      // 模拟发送消息
      this.$message.success('消息发送成功');
      this.messageContent = "";
      
      // 滚动到底部
      this.$nextTick(() => {
        const container = this.$refs.messagesContainer;
        container.scrollTop = container.scrollHeight;
      });
    }
  },
  mounted() {
    // 组件挂载后滚动到底部
    this.$nextTick(() => {
      const container = this.$refs.messagesContainer;
      container.scrollTop = container.scrollHeight;
    });
  }
};
</script>

<style scoped>
.group-chat-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  min-height: 100vh;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  background: rgba(255, 255, 255, 0.2);
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 15px;
  margin: 20px;
}

.group-info {
  display: flex;
  flex-direction: column;
  flex: 1;
  margin: 0 10px;
}

.group-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

.group-members {
  font-size: 12px;
  color: #1890ff;
  cursor: pointer;
  display: flex;
  align-items: center;
}

.group-members .el-icon-user {
  margin-right: 3px;
}

.chat-actions {
  display: flex;
  gap: 10px;
}

.group-announcement {
  padding: 10px 20px;
  background: rgba(255, 255, 255, 0.3);
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 10px;
  margin: 0 20px 20px 20px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.announcement-title {
  font-size: 12px;
  color: #faad14;
  margin-bottom: 5px;
}

.announcement-content {
  font-size: 14px;
  color: #666;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 15px;
  margin: 0 20px 20px 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.message-date {
  text-align: center;
  font-size: 12px;
  color: #999;
  margin-bottom: 20px;
}

.message-item {
  display: flex;
  margin-bottom: 20px;
  max-width: 80%;
}

.message-item.system {
  align-self: center;
  max-width: 100%;
}

.message-item.received {
  align-self: flex-start;
}

.message-item.sent {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  margin: 0 10px;
  object-fit: cover;
}

.message-content {
  display: flex;
  flex-direction: column;
}

.message-item.system .message-content {
  align-items: center;
}

.message-item.received .message-content {
  align-items: flex-start;
}

.message-item.sent .message-content {
  align-items: flex-end;
}

.message-sender {
  font-size: 12px;
  color: #999;
  margin-bottom: 3px;
}

.message-text {
  padding: 10px 15px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.5;
  max-width: 100%;
  word-wrap: break-word;
}

.message-item.system .message-text {
  background: rgba(255, 255, 255, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #faad14;
  display: flex;
  align-items: center;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.message-item.system .message-text .el-icon-bell {
  margin-right: 5px;
}

.message-item.received .message-text {
  background: rgba(255, 255, 255, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-top-left-radius: 0;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.message-item.sent .message-text {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  border-top-right-radius: 0;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.message-time {
  font-size: 12px;
  color: #999;
  margin-top: 5px;
}

.message-item.received .message-time {
  align-self: flex-start;
}

.message-item.sent .message-time {
  align-self: flex-end;
}

.chat-input {
  background: rgba(255, 255, 255, 0.2);
  border-top: 1px solid rgba(255, 255, 255, 0.2);
  padding: 15px 20px;
  border-radius: 15px;
  margin: 0 20px 20px 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.input-actions {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
}

.input-container {
  margin-bottom: 10px;
}

.send-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 10px;
}

.members-list {
  max-height: 400px;
  overflow-y: auto;
}

.member-item {
  display: flex;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

.member-item:last-child {
  border-bottom: none;
}

.member-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 10px;
  object-fit: cover;
  border: 2px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.member-info {
  flex: 1;
}

.member-name {
  font-size: 14px;
  color: #333;
  margin-bottom: 3px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.member-role {
  font-size: 12px;
  color: #666;
}

.member-item:last-child {
  border-bottom: none;
}

.member-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 10px;
  object-fit: cover;
}

.member-info {
  flex: 1;
}

.member-name {
  font-size: 14px;
  color: #333;
  margin-bottom: 3px;
}

.member-role {
  font-size: 12px;
  color: #999;
}
</style>